package com.xuwang.Util;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class DateHandler extends BaseTypeHandler<Date> {
    //    将java类型转换为数据库类型
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        long time=date.getTime();
        preparedStatement.setObject(i,time);
    }
    //将数据库类型转换为java类型 s为想要的字段
    public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
        long time=resultSet.getLong(s);
        Date date = new Date(time);
        return date;
    }

    public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
        long time=resultSet.getLong(i);
        Date date = new Date(time);
        return date;
    }

    public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        long tiem=callableStatement.getLong(i);
        Date date = new Date(tiem);
        return date;
    }


}
